//package com.example.demo.demos.netty;
//
//import io.netty.bootstrap.Bootstrap;
//import io.netty.buffer.ByteBuf;
//import io.netty.buffer.Unpooled;
//import io.netty.channel.*;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.nio.NioSocketChannel;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.boot.context.event.ApplicationStartedEvent;
//import org.springframework.context.event.EventListener;
//import org.springframework.stereotype.Component;
//
//import java.util.concurrent.TimeUnit;
//
//
///**
// * 客户端
// */
//@Component
//@Slf4j
//@RequiredArgsConstructor
//public class Client {
//
//
//    private final  ServerHandlerInit inits;
//
//    public void connect(int port, String host) throws Exception{
//
//        /**
//         * 客户端的NIO线程组
//         *
//         */
//        EventLoopGroup group = new NioEventLoopGroup();
//
//        try {
//            /**
//             * Bootstrap 是一个启动NIO服务的辅助启动类 客户端的
//             */
//            Bootstrap bootstrap = new Bootstrap();
//            /**
//             * 设置group
//             */
//            bootstrap = bootstrap.group(group);
//            /**
//             * 关联客户端通道
//             */
//            bootstrap = bootstrap.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true);
//            /**
//             * 设置 I/O处理类,主要用于网络I/O事件，记录日志，编码、解码消息
//             */
//            bootstrap = bootstrap.handler(inits);
//
//            System.out.println("netty client start success!");
//
//            /**
//             * 连接服务端
//             */
//            log.info("连接服务器尝试");
//            ChannelFuture f = bootstrap.connect(host, port).sync();
////            ChannelFuture f = bootstrap.connect("192.168.1.12", 8802).sync();
//            log.info("连接服务器成功:{}",f);
//            Channel channel = f.channel();
////            ChannelId channelId = channel
////            String queryCommand = "aa0900004000000049bb";
//            ByteBuf buffer = Unpooled.buffer(10);
//            buffer.writeByte(0xaa);
//            buffer.writeByte(0x09);
//            buffer.writeByte(0x00);
//            buffer.writeByte(0x00);
//            buffer.writeByte(0x40);
//            buffer.writeByte(0x00);
//            buffer.writeByte(0x00);
//            buffer.writeByte(0x00);
//            buffer.writeByte(0x49);
//            buffer.writeByte(0xbb);
////            buffer.writeByte(0xaa);
////            buffer.writeByte(0x09);
////            buffer.writeByte(0x00);
////            buffer.writeByte(0x00);
////            buffer.writeByte(0x21);
////            buffer.writeByte(0x01);
////            buffer.writeByte(0x03);
////            buffer.writeByte(0x01);
////            buffer.writeByte(0x2f);
////            buffer.writeByte(0xbb);
//            log.info("发送命令：{}",buffer);
//            ChannelFuture channelFuture = channel.writeAndFlush(buffer);
////            System.out.println("发送命令：" + channelFuture);
////            System.out.println("ChannelId: " + channelId);
//            //通常需要写不断重连服务端
//            f.addListener((ChannelFutureListener) future -> {
//                if (!future.isSuccess()) {
//                    //重连交给后端线程执行
//                    future.channel().eventLoop().schedule(() -> {
//                        log.info("重连服务端...");
//                        try {
//                                log.info("再次执行连接");
//                            connect(port,host);
//                        } catch (Exception e) {
//                            //                        e.printStackTrace();
//                            log.error("连接失败。。。");
//                        }
//                    }, 3000, TimeUnit.MILLISECONDS);
//                } else {
//                    log.info("服务端连接成功...");
//                }
//            });
//            /**
//             * 等待连接端口关闭
//             */
////            f.channel().closeFuture().sync();
//
//        } finally {
//            /**
//             * 退出，释放资源
//             */
////            group.shutdownGracefully();
//        }
//    }
//
//
//
//    /**
//     * 通道初始化
//     */
////    static class ServerHandlerInit extends ChannelInitializer<SocketChannel> {
////        @Override
////        protected void initChannel(SocketChannel ch) throws Exception {
////            ChannelPipeline pipeline = ch.pipeline();
////            // 解码出具体的数据类型
////            //该篇对解码器和编码器进行了详细说明，推荐阅读https://blog.csdn.net/tang_huan_11/article/details/133853786
//////            pipeline.addLast("decoder", new StringDecoder());//解码和编码可以自己定义
//////            pipeline.addLast(new Handler());//handler类是自己写的处理类
//////            pipeline.addLast("encoder", new StringEncoder());
////            ByteBuf delimiter1 = Unpooled.copiedBuffer("aa".getBytes());
////            ByteBuf delimiter2 = Unpooled.copiedBuffer("bb".getBytes());
////            pipeline.addLast(new DelimiterBasedFrameDecoder(1024, delimiter1, delimiter2));
////            pipeline.addLast("decoder", new StringDecoder());//解码和编码可以自己定义
////            pipeline.addLast("encoder", new StringEncoder());
////            pipeline.addLast(new Handler(liftAgvcTaskMapper,liftMapper));
////        }
////    }
//
//    @EventListener(classes = {ApplicationStartedEvent.class
//    })
////    @Async //异步多线程注解
//    //这个方法在服务启动时就会执行，即服务启动后则客户端就启动了
//    public void start() throws Exception {
//            log.info("发送连接ip和端口号");
//            connect(8802,"10.5.24.105");//TCP工具做服务端进行测试的时候的端口和ip
//        }
//    }
